package avatar;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.sql.rowset.CachedRowSet;

import com.game.majiang.algorithm.niuniu.card;
import com.game.server.db.SqlHelper;

public class cardlogicparam {

	public static cardlogicparam Instance;
	public static cardlogicparam getInstance()
	{
		if(Instance == null)
		{
			Instance = new cardlogicparam();
		}
		return Instance;
	}
	public ArrayList<cardlogicdata> chexuan = new ArrayList<>();
	public ArrayList<cardlogicdata> maguxuan = new ArrayList<>();
	public void init()
	{
		String sql = "select * from cardlogicparam";
		CachedRowSet rs = SqlHelper.getResultSet(sql);
		try {
			while (rs.next()) 
			{  
				cardlogicdata data = new cardlogicdata();
				int type  = Integer.parseInt(rs.getString(2));
				data.lunshu  = Integer.parseInt(rs.getString(3));
				data.curbeishu  = Integer.parseInt(rs.getString(4));
				String cardstr  = rs.getString(5);
				String cardss[] = cardstr.split("-");
				for(int i=0; i<cardss.length; i++)
				{
					ArrayList<Integer> a = new ArrayList<>();
					String cards[] = cardss[i].split(",");
					for(int j=0; j<cards.length; j++)
					{
						a.add(Integer.parseInt(cards[j]));
					}
					data.cards.add(a);
				}
				data.betbeushumin  = Integer.parseInt(rs.getString(6));
				data.betbeishumax  = Integer.parseInt(rs.getString(7));
				if(type == 3)
				{
					chexuan.add(data);
				}
				else if(type == 6)
				{
					maguxuan.add(data);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	ArrayList<cardlogicdata> carddata(int type)
	{
		if(type == 3)
		{
			return chexuan;
		}
		else
		{
			return maguxuan;
		}
	}
	
	
	public boolean check(int type, int lun, int bei, ArrayList<card> hand, cardlogicdata t)
	{
		ArrayList<cardlogicdata> data = carddata(type);
		for(int i=0; i<data.size(); i++)
		{
			if(data.get(i).lunshu == lun && bei > data.get(i).curbeishu)
			{
				boolean f = false;
				for(int j=0; j<data.get(i).cards.size(); j++)
				{
					f = ishavecards(data.get(i).cards.get(j), hand);
					if(f)
					{
						break;
					}
				}
				if(!f)
				{
					return false;
				}
				t = data.get(i);
			}
		}
		return true;
	}

	public boolean ishavecards(ArrayList<Integer> card1, ArrayList<card> card2)
	{
		for(int i=0; i<card1.size(); i++)
		{
			boolean f = false;
			for(int j=0; j<card2.size(); j++)
			{
				if(card1.get(i) == card2.get(j).type.get_type())
				{
					f = true;
					if(f)
					{
						break;
					}
				}
			}
			if(!f)
			{
				return false;
			}
		}
		return true;
	}
}
